|
|
@@ -0,0 +1,69 @@
|
|
1
|
+require "twitter"
|
|
2
|
+
|
|
3
|
+module Agents
|
|
4
|
+ class TwitterPublishAgent < Agent
|
|
5
|
+ cannot_be_scheduled!
|
|
6
|
+
|
|
7
|
+ description <<-MD
|
|
8
|
+ The TwitterPublishAgent publishes tweets from the events it receives.
|
|
9
|
+
|
|
10
|
+ You must set up a Twitter app and provide it's `consumer_key`, `consumer_secret`, `oauth_token` and `oauth_token_secret`, (Also shown as "Access token" on the Twitter developer's site.) along with the `username` of the Twitter user to publish as.
|
|
11
|
+
|
|
12
|
+ The `oauth_token` and `oauth_token_secret` specified determine which user the tweet will be sent as.
|
|
13
|
+
|
|
14
|
+ You must also specify a `message_path` parameter: a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value to tweet.
|
|
15
|
+
|
|
16
|
+ Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
|
|
17
|
+ MD
|
|
18
|
+
|
|
19
|
+ def validate_options
|
|
20
|
+ unless options[:username].present? && options[:expected_update_period_in_days].present? && options[:consumer_key].present? && options[:consumer_secret].present? && options[:oauth_token].present? && options[:oauth_token_secret].present?
|
|
21
|
+ errors.add(:base, "expected_update_period_in_days, username, consumer_key, consumer_secret, oauth_token and oauth_token_secret are required")
|
|
22
|
+ end
|
|
23
|
+ end
|
|
24
|
+
|
|
25
|
+ def working?
|
|
26
|
+ (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
|
|
27
|
+ end
|
|
28
|
+
|
|
29
|
+ def default_options
|
|
30
|
+ {
|
|
31
|
+ :username => "",
|
|
32
|
+ :expected_update_period_in_days => "10",
|
|
33
|
+ :consumer_key => "---",
|
|
34
|
+ :consumer_secret => "---",
|
|
35
|
+ :oauth_token => "---",
|
|
36
|
+ :oauth_token_secret => "---",
|
|
37
|
+ :message_path => "text"
|
|
38
|
+ }
|
|
39
|
+ end
|
|
40
|
+
|
|
41
|
+ def receive(incoming_events)
|
|
42
|
+ # if there are too many, dump a bunch to avoid getting rate limited
|
|
43
|
+ if incoming_events.count > 20
|
|
44
|
+ incoming_events = incoming_events.first(20)
|
|
45
|
+ end
|
|
46
|
+ incoming_events.each do |event|
|
|
47
|
+ tweet_text = Utils.value_at(event.payload, options[:message_path])
|
|
48
|
+ begin
|
|
49
|
+ publish_tweet tweet_text
|
|
50
|
+ create_event :payload => {:success => true, :published_tweet => tweet_text, :agent_id => event.agent_id, :event_id => event.id}
|
|
51
|
+ rescue Twitter::Error => e
|
|
52
|
+ create_event :payload => {:success => false, :error => e.message, :failed_tweet => tweet_text, :agent_id => event.agent_id, :event_id => event.id}
|
|
53
|
+ end
|
|
54
|
+ end
|
|
55
|
+ end
|
|
56
|
+
|
|
57
|
+ def publish_tweet text
|
|
58
|
+ Twitter.configure do |config|
|
|
59
|
+ config.consumer_key = options[:consumer_key]
|
|
60
|
+ config.consumer_secret = options[:consumer_secret]
|
|
61
|
+ config.oauth_token = options[:oauth_token]
|
|
62
|
+ config.oauth_token_secret = options[:oauth_token_secret]
|
|
63
|
+ end
|
|
64
|
+
|
|
65
|
+ Twitter.update(text)
|
|
66
|
+ end
|
|
67
|
+
|
|
68
|
+ end
|
|
69
|
+end
|